<template>
	<div class="system-menu-container">
		<div class="breadcrumb-box">
			<Breadcrumb />
		</div>

		<div class="header-search flex space-between">
			<div>
				<el-input class="search-item" placeholder="请输入科室名称或科室代码" v-model="state.apiData.search" size="default"
					style="width: 220px;" clearable @change="searchChange()">
				</el-input>
				<el-button size="default" type="primary" @click="searchChange()" :icon="Search">查询</el-button>
			</div>


			<el-button size="default" type="primary" @click="checkAll()">{{ state.isAllCheck ? '取消全选' : '全选' }}</el-button>

		</div>

		<div class="page-main" v-loading="state.tableData.loading" ref="pageMain">
			<el-card shadow="hover" style="height:100%;">

				<el-table :data="state.tableData.data" border style="width: 100%" :height="state.tableHeight"
					ref="multipleTableRef">
					<el-table-column label="序号" type="index" width="100" show-overflow-tooltip align="center"></el-table-column>
					<el-table-column prop="departmentNo" label="科室代码" show-overflow-tooltip align="center"
						width="150"></el-table-column>
					<el-table-column prop="departmentName" label="科室名称" show-overflow-tooltip align="center"></el-table-column>
					<el-table-column label="一级科室编码" align="center" width="150">
						<template #default="scope">
							{{ scope.row ? '-' : '-' }}
						</template>
					</el-table-column>
					<el-table-column label="一级科室名称" align="center">
						<template #default="scope">
							{{ scope.row ? '-' : '-' }}
						</template>
					</el-table-column>
					<el-table-column label="二级科室编码" align="center" width="150">
						<template #default="scope">
							{{ scope.row ? '-' : '-' }}
						</template>
					</el-table-column>
					<el-table-column label="二级科室名称" align="center">
						<template #default="scope">
							{{ scope.row ? '-' : '-' }}
						</template>
					</el-table-column>
					<el-table-column label="日间标记" align="center" width="100">
						<template #default="scope">
							<el-checkbox v-model="scope.row.isDaySign" @change="daySignChange(scope.row.id, scope.row.isDaySign)">
								{{ }}
							</el-checkbox>
						</template>
					</el-table-column>
				</el-table>


				<div class="pagination-box">
					<el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
						:pager-count="5" :page-sizes="[10, 20, 30]" v-model:current-page="state.apiData.page" background
						v-model:page-size="state.apiData.pageSize" layout="total, sizes, prev, pager, next, jumper"
						:total="state.tableData.total">
					</el-pagination>
				</div>

			</el-card>
		</div>


	</div>
</template>

<script setup name="entryAndExit-operationEntry-index">
import { getCurrentInstance } from 'vue';
import { entryAndExitApi } from '/@/api/entryAndExit'
import {
	Search,
} from '@element-plus/icons-vue'
// 引入组件
const Breadcrumb = defineAsyncComponent(() => import('/@/layout/navBars/breadcrumb/breadcrumb.vue'));

// 定义变量内容
const state = reactive({
	apiData: {
		page: 1,
		pageSize: 10,
		search: '',
	},
	tableData: {
		data: [],
		loading: false,
		total: 0
	},
	tableHeight: '30vh',
	uploadLoading: false,
	isAllCheck: false
});

// 页面加载时
onMounted(() => {
	getList()
	state.tableHeight = getCurrentInstance().refs.pageMain.offsetHeight - 130 + "px";
});

const searchChange = () => {
	state.apiData.page = 1
	getList()
}



// 分页改变
const onHandleSizeChange = (val) => {
	state.apiData.pageSize = val;
	getList();
};
const onHandleCurrentChange = (val) => {
	state.apiData.page = val;
	getList();
};


const daySignChange = (id, val) => {
	const apiData = {
		depIds: [id],
		isDaySign: val
	}
	entryAndExitApi().departmentSetIsDaySign(apiData).then(res => {
		getList()
	})
}



const checkAll = () => {
	const allIdList = []
	state.tableData.data.forEach(item => {
		allIdList.push(item.id)
	})

	const apiData = {
		depIds: allIdList,
		isDaySign: state.isAllCheck ? false : true
	}
	entryAndExitApi().departmentSetIsDaySign(apiData).then(res => {
		getList()
	})
}




// 获取列表
const getList = () => {
	state.tableData.loading = true
	entryAndExitApi().departmentList(state.apiData).then(res => {
		console.log(res)
		state.tableData.loading = false
		state.tableData.data = res.data.items
		state.tableData.total = res.data.total

		let checkedIdList = []
		state.tableData.data.forEach(item => {
			if (item.isDaySign ) {
				checkedIdList.push(item.id)
			}
		})
		if (checkedIdList.length == res.data.items.length) {
			state.isAllCheck = true
		} else {
			state.isAllCheck = false
		}
	}).catch(() => {
		state.tableData.loading = false
	})

}



</script>

<style lang="scss" scoped>
.el-checkbox.el-checkbox--large {
	height: 32px !important;
}

.page-main {
	height: calc(100vh - 50px - 106px);
}
</style>
